//
//  CLControl.m
//  CLBrowser
//
//  Created by chuck on 2021/7/8.
//

#import "CLControl.h"
@interface CLControl()
@property (nonatomic, strong) CALayer * imageLayer;
@property(nonatomic, unsafe_unretained) CGFloat oldAlpha;
@property (nonatomic, assign) CGPoint oldPoint;
@end
@implementation CLControl
{
    UIImage *_image;
    CGPoint _point;
    NSTimer *_timer;
    BOOL _longPressDetected;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    self.oldAlpha = self.alpha;
    _imageFrame = self.bounds;
}

- (CALayer *)imageLayer {
    if (!_imageLayer) {
        _imageLayer = [CALayer layer];
        [self.layer addSublayer:_imageLayer];
    }
    return _imageLayer;
}
- (void)dealloc
{
    [self _endTimer];
}
- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    self.imageFrame = _imageFrame;
}
#pragma mark - Getter & Setter
- (void)setImage:(UIImage *)image {
    _image = image;
    self.imageLayer.contents = (id)image.CGImage;
}
- (void)setImageFrame:(CGRect)imageFrame {
    _imageFrame = imageFrame;
    self.imageLayer.frame = imageFrame;
}

- (UIImage *)image {
    id content = self.imageLayer.contents;
    if (content != (id)_image.CGImage) {
        CGImageRef ref = (__bridge CGImageRef)(content);
        if (ref && CFGetTypeID(ref) == CGImageGetTypeID()) {
            _image = [UIImage imageWithCGImage:ref scale:self.imageLayer.contentsScale orientation:UIImageOrientationUp];
        } else {
            _image = nil;
        }
    }
    return _image;
}


#pragma mark - 私有方法
- (void)_startTimer {
    [_timer invalidate];
    NSLog(@"长按");
    _timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(_timerFire) userInfo:nil repeats:NO];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)_endTimer {
    [_timer invalidate];
    _timer = nil;
}

- (void)_timerFire {
    [self touchesCancelled:[NSSet set] withEvent:nil];
    _longPressDetected = YES;
    if (_longPressBlock) _longPressBlock(self, _point);
    [self _endTimer];
}


#pragma mark - Override
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    self.oldPoint = [touches.anyObject locationInView:self];
    _longPressDetected = NO;
    if (_touchBlock) {
        _touchBlock(self, YYGestureRecognizerStateBegan, touches, event);
    }
    if (_longPressBlock) {
        UITouch *touch = touches.anyObject;
        _point = [touch locationInView:self];
        [self _startTimer];
    }
    if (self.highLightEnable) {
        self.alpha = self.oldAlpha / 4;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    CGPoint curPoint = [touches.anyObject locationInView:self];
    if (_longPressDetected || CGPointEqualToPoint(curPoint, self.oldPoint)) return;
    if (_touchBlock) {
        _touchBlock(self, YYGestureRecognizerStateMoved, touches, event);
    }
    [self _endTimer];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    self.oldPoint = CGPointZero;
    if (_longPressDetected) return;
    if (_touchBlock) {
        _touchBlock(self, YYGestureRecognizerStateEnded, touches, event);
    }
    [self _endTimer];
    if (self.highLightEnable) {
        self.alpha = self.oldAlpha;
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    self.oldPoint = CGPointZero;
    if (_longPressDetected) return;
    if (_touchBlock) {
        _touchBlock(self, YYGestureRecognizerStateCancelled, touches, event);
    }
    [self _endTimer];
    if (self.highLightEnable) {
        self.alpha = self.oldAlpha;
    }
}
@end
